package com.pedro.streamer.view;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.pedro.streamer.R;
import com.pedro.streamer.manager.VideoArrManager;

public class CameraLensControlView extends ConstraintLayout implements CompoundButton.OnCheckedChangeListener {

    private SeekBar filterSpeedBar, filterMarginBar, filterUpdownBar, filterLeftRightBar;
    private CheckBox breathCheckBox, leftRightCheckBox, upDownCheckBox, shakeCheckBox;
    private TextView speedText,marginText;

    private int speed = 50;
    private int margin = 50;
    private int updown = 50;
    private int leftright = 50;
    private VideoArrManager videoArrManager = VideoArrManager.shareInstance();
    private boolean isZoomFilterOpen = false;
    public CameraLensControlView(@NonNull Context context) {
        super(context);
    }

    public CameraLensControlView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        initView();
    }


    public void reloadView(){

        if (breathCheckBox != null){
            breathCheckBox.setChecked(videoArrManager.isZoomFilterRender);
            filterMarginBar.setProgress(videoArrManager.getBreatheMargin());
            filterSpeedBar.setProgress(videoArrManager.getBreatheSpeed());
            speedText.setText(String.valueOf(videoArrManager.getBreatheSpeed()-100));
            marginText.setText(String.valueOf(videoArrManager.getBreatheMargin()));
        }

    }


    private void  initView() {
        breathCheckBox = findViewById(R.id.breath_filte_switch);
        leftRightCheckBox = findViewById(R.id.left_right_filter_switch);
        upDownCheckBox = findViewById(R.id.up_down_filter_switch);
        shakeCheckBox = findViewById(R.id.shake_filter_switch);
        filterSpeedBar = findViewById(R.id.speed_seek_bar);
        filterMarginBar = findViewById(R.id.margin_seek_bar);
        filterUpdownBar = findViewById(R.id.updown_seek_bar);
        filterLeftRightBar = findViewById(R.id.leftright_seek_bar);

        speedText = findViewById(R.id.speed_seekbar_value);
        marginText = findViewById(R.id.margin_seekbar_value);
        speedText.setText(String.valueOf(videoArrManager.getBreatheSpeed()/100));
        marginText.setText(String.valueOf(videoArrManager.getBreatheMargin()));


        reloadView();

        filterSpeedBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                if (!b)
                    return;
                videoArrManager.getZoomFilterRender().setDration(400-i);
                speedText.setText(String.valueOf(i-100));
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                videoArrManager.setBreatheSpeed(seekBar.getProgress());
            }
        });
        filterSpeedBar.setProgress(videoArrManager.getBreatheSpeed());
        filterMarginBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                if (!b)
                    return;
                videoArrManager.getZoomFilterRender().setmMaxAmplitude(i);
                marginText.setText(String.valueOf((i)));


            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                videoArrManager.setBreatheMargin(seekBar.getProgress());

            }
        });
        filterMarginBar.setProgress(videoArrManager.getBreatheMargin());

        filterUpdownBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                updown = seekBar.getProgress();
            }
        });
        filterLeftRightBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                leftright = seekBar.getProgress();
            }
        });


        breathCheckBox.setOnCheckedChangeListener(this);
//        leftRightCheckBox.setOnCheckedChangeListener(this);
//        upDownCheckBox.setOnCheckedChangeListener(this);
//        shakeCheckBox.setOnCheckedChangeListener(this);
    }

    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {


        if (!compoundButton.isPressed())
          return;


        int id = compoundButton.getId();

        if (id == R.id.breath_filte_switch) {
            if (b){
                videoArrManager.getZoomFilterRender().setmMaxAmplitude(videoArrManager.getBreatheMargin());
                videoArrManager.getZoomFilterRender().setmMaxDisplacement(1f);
                videoArrManager.getZoomFilterRender().setmMaxVerticalDisplacement(19f);
                videoArrManager.getZoomFilterRender().setmFrequency(1f);
                videoArrManager.getZoomFilterRender().setDration(videoArrManager.getBreatheSpeed());
                videoArrManager.getOpenGlView().addFilter(videoArrManager.getZoomFilterRender());
            }else {
                videoArrManager.getOpenGlView().removeFilter(videoArrManager.getZoomFilterRender());
            }
            videoArrManager.isZoomFilterRender = b;

        }
//        if (!breathCheckBox.isChecked() && !leftRightCheckBox.isChecked() && !upDownCheckBox.isChecked()) {
//            videoArrManager.getOpenGlView().removeFilter(videoArrManager.getZoomFilterRender());
//            isZoomFilterOpen = false;
//        } else {
//            videoArrManager.getOpenGlView().addFilter(videoArrManager.getZoomFilterRender());
//            if (b) {
//                if (!isZoomFilterOpen) {
//                    videoArrManager.getZoomFilterRender().setmFrequency(speed);
//                    videoArrManager.getZoomFilterRender().setmMaxAmplitude(0);
//                    videoArrManager.getZoomFilterRender().setmMaxDisplacement(0);
//                    videoArrManager.getZoomFilterRender().setmMaxVerticalDisplacement(0);
//                    videoArrManager.getOpenGlView().addFilter(videoArrManager.getZoomFilterRender());
//                    isZoomFilterOpen = true;
//                }
//            }
//
//            if (compoundButton.getId() == R.id.breath_filte_switch) {
//                if (b) {
//                    videoArrManager.getZoomFilterRender().setmMaxAmplitude(margin);
//                } else {
//                    videoArrManager.getZoomFilterRender().setmMaxAmplitude(0);
//                }
//            } else if (compoundButton.getId() == R.id.up_down_filter_switch) {
//                if (b) {
//                    videoArrManager.getZoomFilterRender().setmMaxVerticalDisplacement(updown);
//                } else {
//                    videoArrManager.getZoomFilterRender().setmMaxVerticalDisplacement(0);
//                }
//
//            } else if (compoundButton.getId() == R.id.left_right_filter_switch) {
//                if (b) {
//                    videoArrManager.getZoomFilterRender().setmMaxDisplacement(leftright);
//                } else {
//                    videoArrManager.getZoomFilterRender().setmMaxDisplacement(0);
//                }
//            }
//        }

    }
}
